﻿using System.Linq;
using FluentValidation;
using netCoreStudy.Dto.User;
using netCoreStudy.IService;

namespace netCoreStudy.DtoValidator
{
	public class AddRoleValidator : AbstractValidator<UpdateRoleDto>
	{
		public AddRoleValidator(IRoleService roleService, IMenuService menuService)
		{
			RuleFor(x => x.MenuIds)
				.NotNull()
				.WithName("角色id")
				.WithMessage("{PropertyName}不能为空!")
				.Must(x => x.Count > 0)
				.WithMessage("{PropertyName}至少选择一个角色!")
				.Must(x => x.All(r => menuService.ExistById(r)))
				.WithMessage("{PropertyName}中其中某个角色不存在!")
				;
		}
	}
}